package com.ruoyi.project.content.banner.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.content.banner.domain.Banner;
import com.ruoyi.project.content.banner.service.BannerService;
import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.user.host.domain.Host;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.List;
import java.util.UUID;

/**
 * Banner控制器
 */
@Controller
@RequestMapping("/content/banner")
public class BannerController extends BaseController {

    private String prefix = "content/banner";

    @Autowired
    private BannerService bannerServiceImpl;


    @RequiresPermissions("content:banner:view")
    @GetMapping()
    public String showView() {
        System.out.println("执行了控制器");
        return prefix + "/banner";
    }


    /**
     * 显示页面数据
     *
     * @return
     */
    @RequiresPermissions("context:banner:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo demo(Banner banner) {
        startPage();
        List<Banner> list = bannerServiceImpl.showBanner(banner);
        return getDataTable(list);
    }

    @RequiresPermissions("context:banner:add")
    @GetMapping("/add")
    public String showAdd(){
        return prefix+"/add";
    }




    @RequiresPermissions("context:banner:add")
    @PostMapping("/add2")
    @ResponseBody
    public AjaxResult upLoad(MultipartFile file, HttpServletRequest request, HttpSession session) {
        String uuid = UUID.randomUUID().toString();

        String oldname = file.getOriginalFilename();
        String suffix = oldname.substring(oldname.lastIndexOf("."));
        System.out.println("suffix========="+suffix);

        /**C、文件名称不重复**/
        String filename = uuid + suffix;
        //B、获得服务器的目录
        String realPath = "D:/ruoyi/uploadPath/img";

        System.out.println(realPath);
        // ****A、自动创建目录*******
        File files = new File(realPath);
        if (!files.exists()) {

            files.mkdirs();
        }
        int flag=0;
        try {
            file.transferTo(new File(files, filename));
            flag=1;
        } catch (IOException e) {
            e.printStackTrace();
        }
        session.setAttribute("filename","D:/ruoyi/uploadPath/img/"+filename);
        return toAjax(flag);
    }

    @RequiresPermissions("context:banner:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(Banner banner ,HttpSession session) throws IOException {
        String filename = (String) session.getAttribute("filename");
        banner.setCoverPhoto(filename);
        System.out.println(banner.getBannerTitle()+"----------------");
        return toAjax(bannerServiceImpl.insert(banner));
    }



    @RequiresPermissions("context:banner:remove")
    @Log(title = "banner管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(bannerServiceImpl.deleteBannerByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }


    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("context:banner:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Banner Banner)
    {
        List<Banner> list = bannerServiceImpl.selectBannerList(Banner);
        ExcelUtil<Banner> util = new ExcelUtil<Banner>(Banner.class);
        return util.exportExcel(list, "banner");
    }


    /**
     * 数据回显
     */
    @RequiresPermissions("context:banner:edit")
    @GetMapping("/edit/{bannerId}")
    public String edit(@PathVariable("bannerId") Long bannerId, Model model) throws IOException {
        Banner banner = bannerServiceImpl.selectBannerById(bannerId);

        model.addAttribute("banner",banner);
        return prefix + "/edit";
    }



    //下载指定文件
    @RequestMapping("/getImageByMysql")
    public   void    fileDownLoad( HttpServletResponse resp,String filename) throws IOException {

        //服务器目录


        File  file=new File(filename);

        resp.setContentLength((int) file.length());



        //【A】从服务器中读取需要下载的文件
        InputStream  input=new FileInputStream(file);

        //【B】把文件写到本地硬盘
        OutputStream  ouput=resp.getOutputStream();

        IOUtils.copy(input,ouput);

        ouput.close();

        input.close();


    }




    /**
     * 修改保存【修改banner文章内容】
     */
    @RequiresPermissions("context:banner:edit")
    //@Log(title = "【修改banner文章内容】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Banner banner,HttpSession session)
    {
        String filename = (String) session.getAttribute("filename");
        banner.setCoverPhoto(filename);
        return toAjax(bannerServiceImpl.updateCBanner(banner));
    }

}
